package com.blog.controller;

import com.blog.pojo.Article;
import com.blog.pojo.Tag;
import com.blog.service.ArticleService;
import com.blog.service.TagService;
import com.blog.service.impl.ArticleServiceImpl;
import com.blog.service.impl.TagServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 跳转管理员后台管理页面
 */
@WebServlet("/GoAdminServlet.do")
public class GoAdminServlet extends HttpServlet {

    ArticleService articleService=new ArticleServiceImpl();
    TagService tagService=new TagServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //加载文章
        List<Article> allArticles = articleService.getAllArticles();
        req.setAttribute("articles",allArticles);

        List<String> sorts=new ArrayList<String>();
        //加载所有分类
        Map<String, Integer> map = articleService.getArticleSortAndCount();
        Set<String> keys = map.keySet();
        for (String key : keys) {
            sorts.add(key);
        }
        req.setAttribute("sort",sorts);

        //加载所有标签
        List<Tag> tags = tagService.getTags();
        req.setAttribute("tags",tags);

        req.getRequestDispatcher("/admin/admin.jsp").forward(req,resp);
    }
}
